import os
# 递归遍历目录
def traversal_files(path, templist):
    for dir in os.listdir(path):
        dir = os.path.join(path, dir)
        dir = dir.replace("\\", "/")

        # print(dir)
        # 判断当前目录是否为文件夹
        if os.path.isdir(dir):
            traversal_files(dir, templist)
        else:
            dir_words = dir.split("/")
            dir_index = len(dir_words) - 1
            cur_dir = str(dir_words[dir_index])
            if ("mysql" in cur_dir):
                templist.append(f"mysql -uroot -proot -h127.0.0.1 -D kaer< {dir}\n")


if __name__ == '__main__':
    path = "C:/APS/export_bak_2022_07_22_mysql_fin/mapping"
    templist = []
    traversal_files(path, templist)
    temp = ""
    for t in templist:
        temp += t

    mssql_trigger_create_bat_template = f"@ECHO OFF\n" \
                                        f"{temp}" \
                                        f"ECHO\n" \
                                        f"PAUSE\n" \
                                        f"@ECHO Done"
    print(mssql_trigger_create_bat_template)